﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBaoHanh.Template;
using BussinessLayer;
using QLBaoHanh.NghiepVu;
using DataAccessLayer;

namespace QLBaoHanh.NghiepVu
{
    public partial class frmDsPhienTheoTrangThai : Form
    {
        private IEnumerable<Phien> _phienData;
        public IEnumerable<Phien> PhienData
        {
            get { return _phienData; }
            set 
            { 
                _phienData = value;
                //reset binding
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = _phienData;
            }
        }


        public frmDsPhienTheoTrangThai()
        {
            InitializeComponent();
            cboFilter.SelectedIndex = 0;
            chkChuaHoanThanh.Checked = false;
        }

        #region Methods

        /// <summary>
        /// Add Phien
        /// Create and show form add
        /// </summary>
        private void AddPhien()
        {
            frmPhieuTiepNhan frmObj = new frmPhieuTiepNhan(false, 0, this);
            frmObj.ShowDialog();
            frmObj.Focus();
        }

        /// <summary>
        /// Edit Phien
        /// Create and show form edit
        /// </summary>
        /// <param name="PhienId"></param>
        private void EditPhien(int PhienId)
        {
            if (PhienId == 0)
            {
                //Must choose a row
                MessageBox.Show("Phải chọn lỗi!", "Ds lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                frmPhieuTiepNhan frmObj = new frmPhieuTiepNhan(true, PhienId, this);
                frmObj.ShowDialog();
                frmObj.Focus();
            }
        }

        /// <summary>
        /// Refesh form (grid)
        /// </summary>
        public void RefreshForm()
        {
            LoadDataToGrid();
        }

        /// <summary>
        /// Delete a Phien
        /// </summary>
        /// <param name="PhienId">PhienId</param>
        private void DeletePhien(int PhienId)
        {
            if (PhienId == 0)
            {
                //Must choose a row
                MessageBox.Show("Phải chọn phiên muốn xóa!", "Ds Phiên", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (MessageBox.Show("Bạn chắc chắn muốn xóa?"
                    , "lỗi", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    PhienBO.getInstance.DelPhien(PhienId);
                    RefreshForm();
                }
            }
        }

        /// <summary>
        /// Get selected item of grid view
        /// </summary>
        /// <returns>0 - not selected else return id</returns>
        private int getSelectedId()
        {
            int Id = 0 ;
            if (dataGridView1.SelectedRows.Count != 0)
            {
                Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            }
            return Id;
        }

        /// <summary>
        /// Load data to grid
        /// </summary>
        private void LoadDataToGrid()
        {
            int CurrentRowIndex = 0;
            if (dataGridView1.CurrentRow != null)
            {
                CurrentRowIndex = dataGridView1.CurrentRow.Index;
            }
            //getData with current filter
            GetDataWithCurrentFilter();
            txtTongSoPhien.Text = dataGridView1.Rows.Count.ToString();
            //if current row index !=0 then selected it
            if (CurrentRowIndex != 0)
            {
                this.dataGridView1.Rows[CurrentRowIndex].Selected = true;
            }
            //else Selected to first items.
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count == 0)
            {
                this.dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            }
            //LoadThietBiBaoHanh(getSelectedId());
        }

        private void LoadThietBiBaoHanh(int inPhienId)
        {
            dgvThietBiBaoHanh.AutoGenerateColumns = false;
            if (inPhienId != 0)
            {
                dgvThietBiBaoHanh.DataSource = PhienBO.getInstance.GetThongTinThietBiByPhienId(inPhienId);
            }
        }

        private void LoadNhanVienThucHien(int inPhienId)
        {
            dgvNhanVienThucHien.AutoGenerateColumns = false;
            if (inPhienId != 0)
            {
                dgvNhanVienThucHien.DataSource = PhienBO.getInstance.GetNhanVienThucHienByPhienId(inPhienId);
            }
        }

        public void RefreshGridNhanVienThucHien()
        {
            LoadNhanVienThucHien(getSelectedId());
        }

        public void GetDataWithCurrentFilter()
        {
            string option = cboFilter.SelectedItem.ToString();
            DateTime FromDate = dtpFilter.Value;
            DateTime ToDate = dtpToDate.Value;
            bool OnlyChuaHoanThanh = chkChuaHoanThanh.Checked;
            switch (option)
            {
                case "Ngày":
                    //Loc data trong ngay 
                    //Get phien where date nhan = to date & only chua hoan thanh ko
                    if (OnlyChuaHoanThanh)
                    {
                        PhienData = PhienBO.getInstance.GetPhienInDay(FromDate, true);
                    } 
                    else
                    {
                        PhienData = PhienBO.getInstance.GetPhienInDay(FromDate);
                    }
                    break;
                case "Tháng":
                    //Loc data trong thang
                    //Get phien where thang = thang chon cua FromDate
                    if (OnlyChuaHoanThanh)
                    {
                        PhienData = PhienBO.getInstance.GetPhienInMonth(FromDate, true);
                    }
                    else
                    {
                        PhienData = PhienBO.getInstance.GetPhienInMonth(FromDate);
                    }
                    break;
                case "Từ ngày":
                    //Loc data tu FromDate den ToDate va only chua hoan thanh
                    if (OnlyChuaHoanThanh)
                    {
                        PhienData = PhienBO.getInstance.GetPhienBetween(FromDate, ToDate, true);
                    }
                    else
                    {
                        PhienData = PhienBO.getInstance.GetPhienBetween(FromDate, ToDate);
                    }
                    break;
                default:
                    //Get current day
                    PhienData = PhienBO.getInstance.GetPhienInDay(FromDate);
                    break;
            }
        }

#endregion

        #region Events

        private void frmDSPhienTheoTrangThai_Load(object sender, EventArgs e)
        {
            LoadDataToGrid();   
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddPhien();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EditPhien(getSelectedId());           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DeletePhien(getSelectedId());
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditPhien(getSelectedId());
        }

        private void contextMenu_Add_Click(object sender, EventArgs e)
        {
            AddPhien();
        }

        private void contextMenu_Edit_Click(object sender, EventArgs e)
        {
            EditPhien(getSelectedId());
        }

        private void contextMenu_Delete_Click(object sender, EventArgs e)
        {
            DeletePhien(getSelectedId());
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int rowSelected = e.RowIndex;
                if (e.RowIndex != -1)
                {
                    this.dataGridView1.Rows[rowSelected].Selected = true;
                }
            }
        }
        
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            // nothing is selected
            if (dataGridView1.SelectedRows.Count == 0)
                return;

            if (e.KeyCode == Keys.Enter)
            {
                EditPhien(getSelectedId());
                e.Handled = true;
            }
        }

        private void btnHoanThanh_Click(object sender, EventArgs e)
        {
            int PhienId = getSelectedId();
            if(PhienId == 0)
            {
                return;
            }
            //Check co nhan vien thuc hien truoc khi hoan thanh
            if(dgvNhanVienThucHien.Rows.Count > 0)
            {
                //update hoan thanh cho phien
                Phien inPhien = PhienBO.getInstance.GetPhienById(PhienId);
                frmHoanThanhPhien objFrm = new frmHoanThanhPhien(inPhien, this);
                objFrm.ShowDialog();
            }
            else
            {
                //Thong bao phai chon nhan vien thuc hien truoc khi hoan thanh
                MessageBox.Show("Phải giao nhân viên thực hiện trước khi hoàn thành!"
                    , "Giao nhân viên thực hiện", MessageBoxButtons.OK
                    , MessageBoxIcon.Warning);
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is KhachHang) && (e.ColumnIndex == 6)) //TenKhachHang
            {
                e.Value = (e.Value as KhachHang).Ten;
            }
            else if ((e.Value is KhachHang) && (e.ColumnIndex == 7)) //SDT
            {
                e.Value = (e.Value as KhachHang).Mobile;
            }
            else if (e.Value is NhanVien)
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
            else if (e.Value is TrangThaiPhien)
            {
                if ((e.Value as TrangThaiPhien).Id == (int)Common.TrangThaiPhien.ThaoTacNgay_HoanThanh)
                {
                    e.Value = true;
                }
                else
                {
                    e.Value = false;
                }
                
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int PhienId = getSelectedId();
            LoadThietBiBaoHanh(PhienId);
            LoadNhanVienThucHien(PhienId);
        }

        private void btnGiaoNhanVienThucHien_Click(object sender, EventArgs e)
        {
            int PhienId = getSelectedId();
            if(PhienId!=0)
            {
                frmGiaoNhanVienThucHien frmObj = new frmGiaoNhanVienThucHien(PhienId, this);
                frmObj.ShowDialog();
            }
        }

        private void btnXoaNhanVienThucHien_Click(object sender, EventArgs e)
        {
            if (dgvNhanVienThucHien.SelectedRows.Count != 0)
            {
                //TODO: confirm
                if(MessageBox.Show("Bạn chắc chắn muốn xóa nhân viên thực hiên?"
                    , "Giao nhân viên thực hiện", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    int Phien_NhanVienThucHienId = (int)dgvNhanVienThucHien.SelectedRows[0].Cells["Phien_NhanVienThucHienId"].Value;
                    Phien_NhanVienThucHienBO.getInstance.DelPhien_NhanVienThucHien(Phien_NhanVienThucHienId);
                    //Load lai grid nhan vien thuc hien
                    RefreshGridNhanVienThucHien();
                }
            }
            else
            {

            }

        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }

        private void cboFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            string option = cboFilter.SelectedItem.ToString();
            switch(option)
            {
                case "Ngày":
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "dd/MM/yyyy";
                    dtpToDate.Visible = false;
                    lblToDate.Visible = false;
                    this.chkChuaHoanThanh.Location = new System.Drawing.Point(350, 13);
                    break;
                case "Tháng":
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "MM/yyyy";
                    dtpToDate.Visible = false;
                    lblToDate.Visible = false;
                    break;
                case "Từ ngày":
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "dd/MM/yyyy";
                    dtpToDate.Visible = true;
                    dtpToDate.CustomFormat = "dd/MM/yyyy";
                    lblToDate.Visible = true;
                    this.chkChuaHoanThanh.Location = new System.Drawing.Point(501, 13);
                    break;
                default:
                    dtpFilter.Visible = true;
                    dtpFilter.CustomFormat = "dd/MM/yyyy";
                    dtpToDate.Visible = false;
                    lblToDate.Visible = false;
                    this.chkChuaHoanThanh.Location = new System.Drawing.Point(350, 13);
                    break;
            }
        }

        private void chkChuaHoanThanh_CheckedChanged(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }

        #endregion
    }
}
